package command;

import java.io.IOException;
import java.io.Reader;

import javax.activity.InvalidActivityException;

class ParserLin {
   private Reader in; 
   private char c;
   private String seps;

   ParserLin(Reader in) throws IOException{
      this.in = in;
      c = (char) in.read();
      seps = " ]";
   }

   /**
    * Skips the next ' ' (max at most).
    * 
    * @param max
    * @return the effective number of spaces escaped.
    * 
    * @throws IOException from the reader.
    */
   int skipSpaces(int max) throws IOException{
      int i = 0;
      while(c == ' ' && i++<max)
    		  c = (char) in.read();

      return i;
   }

   void readToken(String token) throws IOException{
      int i = 0;
      do{
         if(token.charAt(i) != c)
            throw new InvalidActivityException();
         System.err.println("--> " + c);
         c = (char) in.read();
      }
      while(++i < token.length());
      System.err.println("--> OK");
   }

   int readInt() throws IOException{
      StringBuilder s = new StringBuilder();
      int n;

      do s.append(c);
      while((c = (char) in.read()) != ' ');

      try{
      n = Integer.parseInt(s.toString());
      }
      catch(NumberFormatException e){
         throw e;
      }

      return n;
   }

   String readWord() throws IOException{
      StringBuilder s = new StringBuilder();

      do s.append(c);
      while(seps.indexOf(c = (char) in.read()) == -1); //while c is not a separator

      return s.toString();
   }

   boolean testChar(char c) {
      return (this.c == c);
   }
   
   char getNextChar(){
	   return this.c;
   }
}
